package com.mrent.service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.mrent.entity.ManagerPayment;
import com.mrent.entity.ManagerShipping;


@Component
public class ManagerShippingServiceImpl implements ManagerShippingService {
	
	
	/**
	 * 保存店主配送方式.
	 * 
	 * @param member
	 */
	public void saveShipping(ManagerShipping managerShipping){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.persist(managerShipping);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 修改店主配送方式.
	 * 
	 * @param member
	 */
	public void updateShipping(ManagerShipping managerShipping){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try {
			trans.begin();
			em.merge(managerShipping);
		}catch(Exception e){
			System.err.println("异常问题");
			e.printStackTrace();
		}
		finally {
			trans.commit();
		}
	}
	
	/**
	 * 获取店主配送方式.
	 * 
	 * @param memberId
	 * @return
	 */
	public ManagerShipping getShipping(Long id){
		EntityManager em = EMF.getEM();
		ManagerShipping managerShipping = null;
		try{
			managerShipping = em.find(ManagerShipping.class, id);
		}catch(Exception e){
			e.printStackTrace();
		}
		return managerShipping;
	}
	
	/**
	 * 店主删除配送方式.
	 * 
	 * @param id
	 */
	public void delShipping(Long id){
		EntityManager em = EMF.getEM();
		EntityTransaction trans = em.getTransaction();
		try{
			trans.begin();
			ManagerShipping managerShipping = em.find(ManagerShipping.class, id);
			if(managerShipping != null){
				em.remove(managerShipping);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			trans.commit();
		}
	}
	
	/**
	 * 店主支付方式列表.
	 * 
	 * @param page
	 * @return
	 */
	public List<ManagerShipping> listShipping(String manager,Page page){
		EntityManager em = EMF.getEM();
		Query query = em.createQuery("select  from ManagerShipping  ms where    ms.manager=:manager");
		query.setParameter("manager", manager);
		page.setTotalCount(query.getResultList().size());
		query.setFirstResult(page.getFirst());
		query.setMaxResults(page.getPageSize());
		List<ManagerShipping> list = query.getResultList();
		return list;
	}

}
